package com.margo.rabbit.springboot.consumer;

import java.io.IOException;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import com.rabbitmq.client.Channel;



/**
 * 	https://blog.csdn.net/en_joker/article/details/91832876
 * 多个消费者消费一个队列， key配置无效
 * @author fangxingbang
 *
 */
@Component
public class Receiver {
	@RabbitListener(bindings = @QueueBinding(value = @Queue(value = "margo-topic-queue", durable = "false"), exchange = @Exchange(value = "margo-topic", durable = "false", type = "topic"), key = "margo-topic"))
	@RabbitHandler // 指定对消息的处理
	public void nackProcess(Channel channel,Message message) throws IOException {
		System.out.println("receive msg : " + new String(message.getBody(),"utf-8"));
		//channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
		channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
	}
	
	
	@RabbitListener(bindings = @QueueBinding(value = @Queue(value = "margo-topic-queue", durable = "false"), exchange = @Exchange(value = "margo-topic", durable = "false", type = "topic"), key = "margo-1"))
	@RabbitHandler // 指定对消息的处理
	public void ackProcess(Channel channel,Message message) {
		try {
			System.out.println("margo-1 receive msg : " + new String(message.getBody(),"utf-8"));
			channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
